# -*- coding: utf-8 -*-
#----------------------------------------------------------------------------#
# customFormat.py
# Lars Yencken <lars.yencken@gmail.com>
# vim: ts=4 sw=4 sts=4 et tw=78:
# Tue Dec 26 16:40:13 2006
#
#----------------------------------------------------------------------------#

""" Additional dictionary formats.
"""

#----------------------------------------------------------------------------#

import format
import bilingualDict
from tools import kana

#----------------------------------------------------------------------------#

class DrEyeCJFormat(format.DictionaryFormat):
    """ The DrEye Chinese to Japanese dictionary.
    """
    #------------------------------------------------------------------------#

    def __init__(self):
        """ Constructor.
        """
        self.name = 'cj_dreye'

    #------------------------------------------------------------------------#

    def matchHeader(self, headerLine):
        """ See parent class.
        """
        return headerLine.rstrip() == 'FORMAT cj_dreye'

    #------------------------------------------------------------------------#

    def parseLine(self, line):
        """ See parent class.
        """
        try:
            chineseWord, japaneseGlosses = line.rstrip().split('\t')
        except ValueError:
            raise format.FormatError, line
        senses = []
        for readingAndSense in japaneseGlosses.split(r'\n\n'):
            if r'\n' in readingAndSense:
                jpReadings, jpSenses = readingAndSense.split(r'\n')

                for reading, sense in zip(
                            jpReadings.split(';'),
                            jpSenses.split(';'),
                        ):
                    if sense:
                        senses.append(sense)
                    else:
                        senses.append(reading)
            else:
                senses.extend(readingAndSense.split(';'))

        return bilingualDict.DictionaryEntry(chineseWord, [chineseWord], senses)

    #------------------------------------------------------------------------#

#----------------------------------------------------------------------------#

class DrEyeJCFormat(format.RegexFormat):
    """ The DrEye Japanese to Chinese dictionary.
    """

    #------------------------------------------------------------------------#

    def __init__(self):
        """ Constructor.
        """
        self.linePattern = re.compile(
            u'^(?P<word>[^\\t]+)\\t(?P<reading>[^\\\\]+).*(?<senses>【.*)$',
            re.UNICODE,
        )
        return

    #------------------------------------------------------------------------#

    def matchHeader(self, headerLine):
        """ See parent class.
        """
        return headerLine.rstrip() == 'FORMAT jc_dreye'

    #------------------------------------------------------------------------#

    def parseLine(self, line):
        """ See parent class.
        """
        if not kana.containsScript(kana.Script.Kanji) and \
                not kana.containsScript(kana.Script.Katakana):
            raise format.DuplicateEntryError

        japaneseWord, remainder = line.rstrip().split()
        readings = []
        senses = []
        for readingAndSense in japaneseGlosses.split(r'\n\n'):
            reading, sense = readingAndSense.split(r'\n')
            readings.append(reading)
            senese.append(sense)

        return bilingualDict.DictionaryEntry(chineseWord, readings, senses)

    #------------------------------------------------------------------------#

#----------------------------------------------------------------------------#

